package code;

/**
 * 描述:给定一个正整数arr，找出该数组中两个元素相减的最大值，其中被减数的下标不小于减数的下标
 *
 * @author
 * @created 2022/1/11 19:28
 */
public class MaxDiff {

    public static void main(String[] args) {
        int[] arr = {67,43,5,1,7,5,999,43,2,2};
        System.out.println(maxDiff(arr));
    }

    public static int maxDiff(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (max < arr[j] - arr[i]) {
                    max = arr[j] - arr[i];
                }
            }
        }
        return max;
    }

    public static int maxDiff1(int[] arr) {
        int min = arr[0];
        int max = arr[1];
        for (int i = 2; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
            if (min > arr[i - 1]) {
                min = arr[i - 1];
            }
        }
        return max - min;
    }

}
